package com.sbox.album;

import com.sbox.album.model.AlbumContext;
import com.sbox.album.model.AlbumInfo;
import com.sbox.util.AlbumConst;
import com.sbox.widget.AlbumListView;
import com.sbox.widget.TitleBar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

public class AlbumsView extends LinearLayout {
	private Context mContext = null;
	private Activity mActivity = null;
	private TitleBar mTitleBar = null;
	private AlbumListView mAlbumList = null;	
	
	private View.OnClickListener mAddAlbumListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) {
			Context c = AlbumsView.this.mContext;			
			Intent i = new Intent(c, CreateAlbumActivity.class);
			i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
			AlbumsView.this.mActivity.startActivityForResult(i, AlbumConst.REQUEST_CREATE_ALBUM);
		}
	};
	
	private AdapterView.OnItemClickListener mAlbumListener = new AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			AlbumListView.AlbumAdapter adapter = null;
			AlbumInfo album = null;
			Context c = AlbumsView.this.mContext;
			
			adapter = (AlbumListView.AlbumAdapter)parent.getAdapter();
			album = (AlbumInfo)adapter.getItem(position);
			if( album == null )
				return;			

			//set album:
			AlbumContext.setAlbum(album);
			
			Intent i = new Intent(c, AlbumActivity.class);
			AlbumsView.this.mActivity.startActivityForResult(i, AlbumConst.REQUEST_VIEW_ALBUM );
			
		}		
	};
	
	public AlbumsView(Context context) {
		super(context);
	}

	public AlbumsView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	
	public void init(Activity activity)
	{
		mContext = this.getContext();
		mActivity = activity;
		
		//init the title bar:
		mTitleBar = (TitleBar)findViewById(R.id.albums_title_bar);
		mTitleBar.getButton2().setImageResource(R.drawable.btn_albums);
		mTitleBar.getButton2().setOnClickListener(mAddAlbumListener);

		
		
		//init the album list view:
		mAlbumList = (AlbumListView)findViewById(R.id.albums_list); 
		mAlbumList.setOnItemClickListener(mAlbumListener);
		mAlbumList.loadAlbums();
	}
}
